package com.stylefeng.guns.modular.pay.controller.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.util.AjaxRes;
import com.stylefeng.guns.core.util.DateUtil;
import com.stylefeng.guns.core.util.IdGenerator;
import com.stylefeng.guns.modular.pay.model.Member;
import com.stylefeng.guns.modular.pay.model.WechatApp;
import com.stylefeng.guns.modular.pay.service.IMemberService;
import com.stylefeng.guns.modular.pay.service.IWechatAppService;
import com.stylefeng.guns.modular.pay.service.impl.IdWorkerService;
import com.stylefeng.guns.modular.pay.util.HttpTool;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;

/**
 * 首页
 * @author Administrator
 *
 */
@Api(value="首页相关业务接口",tags="首页相关业务接口")
@Controller
@RequestMapping("/api/index")
public class ApiIndexController extends BaseController{
	private static final String CODEURL = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect";
	@Autowired
	private IWechatAppService wechatAppService;
	@Autowired
	private IMemberService memberService;
	@Autowired
	private IdWorkerService idWorkerService;
	
	
	@ApiOperation(value="商户信息获取", notes="商户信息获取")
	@RequestMapping(value = "/memberInfo",method = RequestMethod.GET)
	@ApiImplicitParam(name = "code", value = "授权code", required = true, dataType = "String")
	@ResponseBody
	public AjaxRes index(@RequestParam("code")String code) {
		WechatApp app = wechatAppService.selectById(1);
		//获取access_token
		JSONObject accessToken = HttpTool.getAccessToken(app.getAppId(), app.getAppSecret(),code);
		if(accessToken.containsKey("errcode")){
			String errcode = accessToken.getString("errcode");
			String errmsg = accessToken.getString("errmsg");
			return new AjaxRes(errcode, errmsg);
		}
		String access_token = accessToken.getString("access_token");
		String openid = accessToken.getString("openid");
		
		//获取用户信息
		JSONObject userInfo = HttpTool.getUserDetail(access_token, openid);
		if(userInfo.containsKey("errcode")){
			String errcode = userInfo.getString("errcode");
			String errmsg = userInfo.getString("errmsg");
			return new AjaxRes(errcode, errmsg);
		}
		Member member = memberService.selectOne(new EntityWrapper<Member>().eq("openid", openid));
		if(member==null){
			member = new Member();
			member.setOpenid(openid);
			member.setMemberNo(idWorkerService.idGenerator());
			member.setNickname(userInfo.containsKey("nickname")?userInfo.getString("nickname"):"");
			member.setSex(userInfo.containsKey("sex")?userInfo.getString("sex"):"");
			member.setProvince(userInfo.containsKey("province")?userInfo.getString("province"):"");
			member.setCity(userInfo.containsKey("city")?userInfo.getString("city"):"");
			member.setCountry(userInfo.containsKey("country")?userInfo.getString("country"):"");
			member.setHeadimgurl(userInfo.containsKey("headimgurl")?userInfo.getString("headimgurl"):"");
			member.setUnionid(userInfo.containsKey("unionid")?userInfo.getString("unionid"):"");
			member.setAuthStatus(0);
			memberService.insert(member);
		}else{
			if(!member.getOpenid().equals(openid)){
				member.setOpenid(openid);
				memberService.updateById(member);
			}
		}
		return new AjaxRes(AjaxRes.SUCCESS, "",member);
	
	}
	
}
